<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\admin\api\SlideApi;
use app\portal\model\JubaoModel;
use app\portal\model\PortalCategoryModel;
use app\portal\model\PortalPostModel;
use cmf\controller\HomeBaseController;

class IndexController extends HomeBaseController
{
    public function index()
    {
        $siteInfo = cmf_get_site_info();
        $this->assign("title", '首页-'.$siteInfo['site_name']);
        $this->assign("description", "首页");
        $this->assign("keyword", "首页");

        // 幻灯片
        $slide = new SlideApi();
        $slideList = $slide->getSlideList(['slide_id' =>1]);
//        dump($slideList->toArray());die();
        $this->assign("slideList", $slideList);

        // 党务公开
        $post = new PortalPostModel();

        $dwCateId = 12;
        $cateGategoryModel = new PortalCategoryModel();
        $sonIds = $cateGategoryModel->getSonCateIds($dwCateId);
        array_push($sonIds, $dwCateId);
        $dwCateIds = implode(',', $sonIds);
        $dwPost = $post->query("select p.* from xg_portal_post as p left join xg_portal_category_post as cp on p.id = cp.post_id where cp.category_id in({$dwCateIds}) and post_status = 1 and recommended = 1 order by p.update_time desc limit 1; ");
        if(is_array($dwPost) && isset($dwPost[0])) {
            $dwPost = $dwPost[0];
            $more = json_decode($dwPost['more'],1);
            $dwPost['thumb'] = $more['thumbnail'] ? '/upload/'. $more['thumbnail'] : '';
        }
        $pattern_src = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/';
        $dwPost['post_content'] = preg_replace_callback($pattern_src, function ($item) {
            return '';
        }, $dwPost['post_content']);
        $dwPost['post_excerpt'] = $dwPost['post_content'];
        $this->assign("dwPost", $dwPost);

        // 广告位1
        $ad1 = $slide->getSlideList(['slide_id' => 2]);
        $this->assign("ad1", $ad1 ? $ad1[0] : []);

        $ad2 = $slide->getSlideList(['slide_id' => 3]);
        $this->assign("ad2", $ad2 ? $ad2[0] : []);

        // 国土新闻
        $gtCateId = 14;
        $postCate = new PortalCategoryModel();
        $orderBy = "order by p.recommended desc, p.update_time desc";
        $where = "cp.category_id = {$gtCateId}";
        $gtList = $postCate->getPostList($where, $orderBy, 6);
        $this->assign("gtList", $gtList);

        // 延吉市地价
        $where = "cp.category_id = 15";
        $yjList= $postCate->getPostList($where, $orderBy,3);
        $this->assign("yjList", $yjList);

        // 财务公开
        $where = "cp.category_id = 28";
        $cwList= $postCate->getPostList($where, $orderBy,6);
        $this->assign("cwList", $cwList);

        // 举报公开
//        $where = "cp.category_id = 16";
//        $jbList= $postCate->getPostList($where, $orderBy,4);
        $jubaoModel = new JubaoModel();
        $jbList = $jubaoModel->where("show_in_index = 1")->order("id", "desc")->limit(0,4)->select();
        $this->assign("jbList", $jbList);

        // 土地
        $where = "cp.category_id = 18";
        $tdList= $postCate->getPostList($where, $orderBy,4);
        $this->assign("tdList", $tdList);

        // 矿产
        $where = "cp.category_id = 19";
        $ccList= $postCate->getPostList($where, $orderBy,4);
        $this->assign("ccList", $ccList);

        // 房产
        $where = "cp.category_id = 20";
        $fcList= $postCate->getPostList($where, $orderBy,4);
        $this->assign("fcList", $fcList);

        // 测绘
        $where = "cp.category_id = 21";
        $chList= $postCate->getPostList($where, $orderBy,4);
        $this->assign("chList", $chList);

        // 公告
        list($gsList, $tzList, $gsSonCateList, $tzSonCateList) = $this->getNoticList();
        $this->assign("gsList", $gsList);
        $this->assign("tzList", $tzList);
        $this->assign("gsSonCateList", $gsSonCateList);
        $this->assign("tzSonCateList", $tzSonCateList);

        // 专题专项
//        $zhuantiList = $slide->getSlideList(['slide_id' => 4]);
//        $this->assign("zhuantiList", $zhuantiList);

        // 专题
        $zhuanti = new PortalPostModel();
        $zhuantiList = $zhuanti->where("post_type = 2 and delete_time = 0")->order("update_time",'desc')->limit(4)->select()->toArray();

        if(is_array($zhuantiList)) {
            $zhuantiList = array_map(function ($item) {
                $more = $item['more'];
                $item['thumb'] =$more['thumbnail'] ? '/upload/'. $more['thumbnail'] : '';
                return $item;
            }, $zhuantiList);
        }
        $this->assign("zhuantiList", $zhuantiList);
        return $this->fetch(':index');
    }

    // 获取公告新闻
    public function getNoticList() {
        $postCate = new PortalCategoryModel();
        $gsId = 22; // 公告公示分类id
        $tzId = 23 ; // 公告通知id
        // 获取子id
        $categoryModel = new PortalCategoryModel();
        $gsSonList = $categoryModel->where("parent_id", '=', $gsId)->limit(4)->select()->toArray();
        $tzSonList = $categoryModel->where("parent_id", '=', $tzId)->limit(4)->select()->toArray();

        $gsList = [];
        $orderBy = "order by p.recommended desc, p.update_time desc";

        foreach ($gsSonList as $cate) {
            $where = "cp.category_id = {$cate['id']}";
            $list = $postCate->getPostList($where, $orderBy, 8);
            $gsList[] = $list;
        }

        $tzList = [];
        foreach ($tzSonList as $cate) {
            $where = "cp.category_id = {$cate['id']}";
            $list = $postCate->getPostList($where, $orderBy, 8);
            $tzList[] = $list;
        }
        return [$gsList, $tzList, $gsSonList, $tzSonList];
    }

    public function search() {
        $this->assign('title', '搜索结果');
        $this->assign('keyword', '搜索结果');
        $this->assign('description', '搜索结果');
        $keyword = $this->request->get("keyword");
        if(!$keyword) {
            return $this->error('请输入关键字查询');
        }

        $post = new PortalPostModel();
        $result = $post->whereLike("post_title", "%{$keyword}%")->paginate(10, false , ['query' => ['keyword' => $keyword]]);
        $this->assign('result', $result);
        return $this->fetch(":search");

    }

    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $ext = strtolower($file->getExtension());
        $size = $file->getSize();
        if($size > 2048000) {
            return $this->error('上传文件不能超过2M');
        }

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $dir = ROOT_PATH . 'public' . DS . 'upload';
            $info = $file->move($dir);
            $ext = $info->getExtension();
            if(!in_array($ext, ['jpg','jpeg','png','gif','bmp','doc','docx','xls','xlsx'])) {
                return $this->error('只允许上传图片或者word或者excel文件'.$ext);
            }
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $path = 'upload'.DS.$info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                echo $info->getFilename();
                $data = [
                    'ext' => $ext,
                    'path' => $path,
                    'origin_name' => $file->getFilename()
                ];
                return $this->success('',null,$data);
            }else{
                // 上传失败获取错误信息
//                echo $file->getError();
                $this->error($file->getError());
            }
        }
    }
}
